﻿// Program.cs
//
// Andrew Bradnan 2009-2010
// andrew.bradnan@gmail.com

using System;

using Path = System.IO.Path;

using Antlr.Runtime;
using Antlr.Runtime.Tree;
using Antlr.Runtime.Debug;

namespace Browse
{
	class Program
	{
		static string[] _args;
		protected static void PrintStats(object sender, ConsoleCancelEventArgs args)
		{
			Console.WriteLine("Parsed {0} of {1} files. ({2}%)", count, files_total, (float)count * 100.0 / (float)files_total);
		}

		static int count = 0;
		static int files_total = 0;
		static void Main(string[] args)
		{
			Parse parser = new Parse();
			// Check usage.
			if (args.Length == 0)
			{
                Console.Error.WriteLine("Usage: browse <input-file> -q -n -a -D MACRO");
                Console.Error.WriteLine("-q go Quick, don't wait if errors");
                Console.Error.WriteLine("-n print Nodes");
                Console.Error.WriteLine("-a skip files with Archive attribute set");
				return;
			}
			_args = args;

			Console.CancelKeyPress += new ConsoleCancelEventHandler(PrintStats);

			try
			{
				string searchPattern = Path.GetFileName(args[0]);
				string dir = string.Empty;

				dir = Path.GetDirectoryName(args[0]);
				if (dir == string.Empty)
					dir = Environment.CurrentDirectory;
				
				parser.ParseDirectory(dir, searchPattern, ref count, ref files_total);
			}
			catch (ApplicationException ae)
			{
				Console.WriteLine(ae.Message);
				Console.ForegroundColor = ConsoleColor.White;
				return;
			}
			finally
			{
				Console.WriteLine("Parsed {0} of {1} files. ({2}%)", count, files_total, (float)count * 100.0 / (float)files_total);
			}
			
            // Pause if debugging so the console doesn't disappear.
            if (System.Diagnostics.Debugger.IsAttached)
			{
				Console.WriteLine("Done...press the any key :)");
				Console.ReadKey();
			}
		}
	}
}
